package com.basic.modules.nykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.*;
import com.basic.modules.nykf.dao.ZzhDao;
import com.basic.modules.nykf.entity.ZzhEntity;
import com.basic.modules.nykf.entity.ZzhFpGsEntity;
import com.basic.modules.nykf.entity.ZzhFpKfEntity;
import com.basic.modules.nykf.service.ZzhFpGsService;
import com.basic.modules.nykf.service.ZzhFpKfService;
import com.basic.modules.nykf.service.ZzhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ZzhServiceImpl extends ServiceImpl<ZzhDao, ZzhEntity> implements ZzhService {

    @Autowired
    ZzhDao zzhDao;

    @Lazy
    @Autowired
    ZzhFpGsService zzhFpGsService;

    @Lazy
    @Autowired
    ZzhFpKfService zzhFpKfService;

    @Override
    public PageUtils getZzhGlList(ParamsModel<Map> params) {
        IPage<Map> page = new Query<Map>().getPage(BeanUtil.beanToMap(params.getPage()));
        IPage<Map> zzhFpgsList = baseMapper.getZzhGlList(page, params.getCondition());
        return new PageUtils(zzhFpgsList);
    }

    @Override
    public R postZzhStop(Map map) {
        // 停用子账号校验,当天不能被分配,判断时间是否重叠
        Long zzhId = Long.parseLong(map.get("zzhId").toString());
        Date toDay = DateUtils.stringToDate(map.get("jsrq").toString(), DateUtils.DATE_PATTERN);

        QueryWrapper<ZzhFpGsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZzhFpGsEntity::getZzhId, zzhId)
                .orderByAsc(ZzhFpGsEntity::getKsrq);

        List<ZzhFpGsEntity> list = zzhFpGsService.list(queryWrapper);

        for (ZzhFpGsEntity zzhFpGsEntity : list) {
            // 校验分配公司
            Date ksrq = zzhFpGsEntity.getKsrq();
            Date jsrq = zzhFpGsEntity.getJsrq();
            if (jsrq.getTime() != 6879398400000L) {
                if (toDay.compareTo(ksrq) >= 0 && toDay.compareTo(jsrq) <= 0) {
                    return R.error("当前停止时间已被分配公司,无法停用");
                }
            } else {
                if (toDay.compareTo(ksrq) < 0) {
                    return R.error("当前停止时间已被分配公司,无法停用");
                } else {

                    // 判断是否分配了客服
                    QueryWrapper<ZzhFpKfEntity> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.lambda().eq(ZzhFpKfEntity::getFpGsId, zzhFpGsEntity.getFpGsId());
                    queryWrapper1.lambda().gt(ZzhFpKfEntity::getKsrq, toDay);
                    int count = zzhFpKfService.count(queryWrapper1);
                    if (count > 0) {
                        return R.error("分配失败,该日期已被分配客服！");
                    } else {
                        // 修改分配公司的2188
                        UpdateWrapper<ZzhFpGsEntity> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("fp_gs_id", zzhFpGsEntity.getFpGsId());
                        updateWrapper.set("jsrq", toDay);
                        zzhFpGsService.update(updateWrapper);

                        // 修改分配客服的2188
                        UpdateWrapper<ZzhFpKfEntity> updateWrapper1 = new UpdateWrapper<>();
                        updateWrapper1.lambda()
                                .eq(ZzhFpKfEntity::getFpGsId, zzhFpGsEntity.getFpGsId())
                                .eq(ZzhFpKfEntity::getJsrq, DateUtils.stringToDate("2188-01-01", DateUtils.DATE_PATTERN))
                                .set(ZzhFpKfEntity::getJsrq, toDay);
                        zzhFpKfService.update(updateWrapper1);
                    }
                }
            }
        }

        // 把子账号状态改为停用
        UpdateWrapper<ZzhEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("zzh_id", zzhId);
        updateWrapper.set("zzh_status", 0);
        boolean update = this.update(updateWrapper);
        if (update) {
            return R.ok();
        } else {
            return R.error("停用失败");
        }
    }

    @Override
    public R postZzhEnable(Map map) {
        System.out.println(map);
        Long zzhId = Long.parseLong(map.get("zzhId").toString());
        UpdateWrapper<ZzhEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(ZzhEntity::getZzhId, zzhId)
                .set(ZzhEntity::getZzhStatus, 1);
        boolean update = this.update(updateWrapper);
        if (update) {
            return R.ok();
        } else {
            return R.error("启用失败");
        }
    }

    @Override
    public List<ZzhEntity> getZzhList(Map map) {
        return zzhDao.getZzhList(map);
    }
}